﻿using System.Collections.Generic;
using System.Linq;
using NFine.Code;
using NFine.Data;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain._02_ViewModel;
using NFine.Domain._03_Entity.InformationCenter;
using NFine.Domain._04_IRepository.InformationCenter;

namespace NFine.Repository.InformationCenter
{
    public class SysMessagesRepository : RepositoryBase<SysMessagesEntity>, ISysMessagesRepository
    {
        public int DeleteMessages(string keyValues)
        {
            try
            {
                //开启事务
                using (var db = new RepositoryBase().BeginTrans())
                {
                    db.Delete<SysMessagesEntity>(t => keyValues.Contains(t.F_Id));
                    return db.Commit();
                }
            }
            catch
            {
                return 0;
            }
        }

        public List<View_Messages> GetMessageList(Pagination pagination, bool isRead, bool isAdmin, string userId,
            string keyword = "")
        {
            var takeCount = pagination.rows * pagination.page;
            var skipCount = pagination.rows * (pagination.page - 1);
            var messages = dbcontext.Set<SysMessagesEntity>().AsQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                messages = messages.Where(t => t.F_Title.Contains(keyword));
            }
            if (!isAdmin) //如果不是管理员查看，只能查看自己发的留言
            {
                messages = messages.Where(t => t.F_CreatorUserId == userId);
                if (!isRead) // 如果查看的是未读留言列表，排除掉最后回复是自己的数据
                {
                    messages =
                        messages.Where(
                            t => t.F_IsRead == false && t.F_LastModifyUserId != null && t.F_LastModifyUserId != userId);
                }
            }
            else
            {
                messages = messages.Where(t => t.F_IsRead == isRead);
            }
            var datas = from m in messages
                join u in dbcontext.Set<UserEntity>().AsQueryable()
                on m.F_CreatorUserId equals u.F_Id
                select new View_Messages
                {
                    F_CreatorUserId = u.F_Id,
                    F_Contact = m.F_Contact,
                    F_Id = m.F_Id,
                    F_Title = m.F_Title,
                    F_CreatorUserName = u.F_RealName,
                    F_Content = m.F_Content,
                    F_CreatorTime = m.F_CreatorTime
                };
            return datas.OrderByDescending(o => o.F_CreatorTime).Skip(skipCount).Take(takeCount).ToList();
        }

        public List<SysMessagesEntity> GetMemberUnReadList(int topCount, string userId)
        {
            var messages =
                dbcontext.Set<SysMessagesEntity>()
                    .AsQueryable()
                    .Where(
                        t =>
                            t.F_CreatorUserId == userId && t.F_IsRead == false && t.F_LastModifyUserId != null &&
                            t.F_LastModifyUserId != userId);
            return messages.Take(topCount).ToList();
        }

        public View_Messages GetViewModel(string keyValue)
        {
            var model = from m in dbcontext.Set<SysMessagesEntity>().AsQueryable().ToList()
                join u in dbcontext.Set<UserEntity>().AsQueryable().ToList()
                on m.F_CreatorUserId equals u.F_Id
                where m.F_Id == keyValue
                select new View_Messages
                {
                    F_Id = m.F_Id,
                    F_Content = m.F_Content,
                    F_CreatorTime = m.F_CreatorTime,
                    F_CreatorUserName = u.F_RealName,
                    F_Title = m.F_Title,
                    F_Contact = m.F_Contact,
                    F_CreatorUserId = u.F_Id
                };
            return model.FirstOrDefault();
        }

        /// <summary>
        /// 获取导航图片
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public NavigationImageEntity getImages()
        {
            var list = dbcontext.Database.SqlQuery<NavigationImageEntity>(string.Format("select * from navigationimage")).ToList();
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                var entity = new NavigationImageEntity();
                return entity;
            }
        }
        /// <summary>
        /// 保存导航图片
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public int saveImages(NavigationImageEntity entity)
        {
            return dbcontext.Database.ExecuteSqlCommand(string.Format("update navigationimage set F_Image1='{0}',F_Image2='{1}',F_Image3='{2}',F_Image4='{3}',F_Image5='{4}' where F_Id ={5}", entity.F_Image1, entity.F_Image2, entity.F_Image3, entity.F_Image4, entity.F_Image5, '4'));
            
        }
    }
}